﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using System.Transactions;
using Chinaer.Core.Helper;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class ShangJiaDAO : Repository<Model.Entities.Model_ShangJia>, IShangJiaDAO
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override bool Add(Model.Entities.Model_ShangJia entity)
        {
            if (CheckIsNullHelper<Model.Entities.Model_ShangJia>.CheckIsNull(entity))
            {
                Chinaer.Core.Helper.ExceptonHelper.ThrowException("商家实体不能为空");
            }

            return base.Add(entity);
        }
        /// <summary>
        /// 根据实体删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override bool Delete(Model.Entities.Model_ShangJia entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            entity.IsUse = false;
            return base.Update(entity);
        }
        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override Model.Entities.Model_ShangJia GetByID(string id)
        {
            Model.Entities.Model_ShangJia shangJia = new Model.Entities.Model_ShangJia();
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                shangJia = context.ShangJias.FirstOrDefault(p => p.ID == id && p.IsUse == true && p.IsDelete == false);
            }
            return shangJia;
        }
        /// <summary>
        /// 根据Id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override bool Delete(string id)
        {
            Model.Entities.Model_ShangJia shangJia = GetByID(id);
            return base.Delete(shangJia);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from s in context.ShangJias
                                  where listID.Contains(s.ID) && s.IsUse==true&&s.IsDelete==false
                                  select s;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_ShangJia>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                    item.IsUse = false;
                }
                return context.SaveChanges();
            }
        }

        public override bool Exists(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ShangJias.FirstOrDefault(p => p.ID == id && p.IsUse == true && p.IsDelete == false) != null ? true : false;
            }
        }
        public override bool Update(Model.Entities.Model_ShangJia entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }

        public Model.Entities.Model_ShangJia GetByName(string name)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ShangJias.FirstOrDefault(p => p.LoginName.Equals(name, StringComparison.InvariantCultureIgnoreCase));
            }
        }


        public new IList<Model.Entities.Model_ShangJia> Query(string sql, int pageIndex, out int recordCount, [System.Runtime.InteropServices.OptionalAttribute][System.Runtime.InteropServices.DefaultParameterValueAttribute(20)]int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }


        public List<Model.Entities.Model_ShangJia> GetAllShangJias()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ShangJias.Where(t => t.IsUse == true && t.IsDelete == false).ToList();
            }
        }
    }
}
